package health.lm.com.mine.pop.qrCode;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.widget.CustomPopWindow;
import health.lm.com.mine.R;

/**
 * 分享弹出框
 */

public class QrCodePopUtil implements View.OnClickListener {
    private static QrCodePopUtil qrCodePopUtil;
    private static Context ctx;
    private CustomPopWindow popWindow;
    private String url;
    private View mRvHome;
    private ImageView img;
    private View contentView;

    public static QrCodePopUtil getInstance() {
        if (qrCodePopUtil == null) {
            qrCodePopUtil = new QrCodePopUtil();
        }
        return qrCodePopUtil;
    }

    /**
     * 显示popwindow
     */
    public void show(Context context, View mLayoutPopup,String url) {
        this.mRvHome = mLayoutPopup;
        ctx = context;
        this.url = url;
        this.contentView = LayoutInflater.from(context).inflate(R.layout.pop_qr_code, null);
        //图片
        img = contentView.findViewById(R.id.iv_img);
        img.setOnClickListener(this);
        ViewGroup.LayoutParams lp = img.getLayoutParams();
        lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
        lp.height = LinearLayout.LayoutParams.MATCH_PARENT;
        img.setLayoutParams(lp);
        //下载
        TextView download = contentView.findViewById(R.id.tv_download);
        download.setOnClickListener(this);
        ImageLoaderHelper.getInstance().load(ctx,url,img);
        showPop();
    }


    private void showPop() {
        popWindow = new CustomPopWindow.PopupWindowBuilder(ctx)
                .setView(contentView)
                .enableBackgroundDark(true)
                .setBgDarkAlpha(0.7f)
                .setAnimationStyle(R.style.pop_bottom_anim)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                .create()
                .showAtLocation(mRvHome, Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_download) download1();
        else if (v.getId() == R.id.iv_img) popWindow.dissmiss();
        else popWindow.dissmiss();
//        switch (v.getId()) {
//            case R.id.tv_download://下载
//                download1();
//                break;
//            case R.id.iv_img://下载
//                popWindow.dissmiss();
//                break;
//            default:
//                popWindow.dissmiss();
//                break;

//        }
    }
    private void download1(){
        Glide.with(ctx)
                .asFile()
                .load(url)
                .into(new SimpleTarget<File>() {
                    @Override
                    public void onResourceReady(@android.support.annotation.NonNull File resource, @Nullable Transition<? super File> transition) {
                        //获取到下载得到的图片，进行本地保存
                        File pictureFolder = Environment.getExternalStorageDirectory();
                        //第二个参数为你想要保存的目录名称
                        File appDir = new File(pictureFolder, "ShangJiYin");
                        if (!appDir.exists()) {
                            appDir.mkdirs();
                        }
                        String fileName = System.currentTimeMillis() + ".jpg";
                        File destFile = new File(appDir, fileName);
                        //把gilde下载得到图片复制到定义好的目录中去
                        copy(resource, destFile);
                        // 最后通知图库更新
                        ctx.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                Uri.fromFile(new File(destFile.getPath()))));

                    }
                });
    }
    /**
     * 复制文件
     *
     * @param source 输入文件
     * @param target 输出文件
     */
    public void copy(File source, File target) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(source);
            fileOutputStream = new FileOutputStream(target);
            byte[] buffer = new byte[1024];
            while (fileInputStream.read(buffer) > 0) {
                fileOutputStream.write(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
                popWindow.dissmiss();
                ToastUtils.showShort("下载成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
